package com.hp.bon.logreverse.perf;

import com.hp.bon.logreverse.ILogFileRead;
import com.hp.bon.logreverse.LogDirScan;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PerfTestLogDirScan extends LogDirScan
{
  private int waitSecond = 60;

  private static final Logger LOG = LoggerFactory.getLogger(PerfTestLogDirScan.class);

  private String contextFileName = "./DirScanContext.txt";
  private File contextFile;
  private String lastProcessedFile = null;

  public void setWaitSecond(int waitSecond)
  {
    this.waitSecond = waitSecond;
  }

  public void run()
  {
    LOG.info("PerfTestLogDirScan thread start..");
    init();
    String startDay = null;
    String endDay = null;
    int startSeq = -1;
    this.lastProcessedFile = readFile(this.contextFile);

    if (this.lastProcessedFile != null)
    {
      startDay = getFileDay(this.lastProcessedFile);
      startSeq = getFileSeq(this.lastProcessedFile);
    }
    findFileByStartAndEnd(startDay, startSeq, endDay, -1);
    try {
      Thread.sleep(this.waitSecond * 1000);
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    LOG.info("No file to process,PerfTestLogDirScan thread stopped");
    System.exit(0);
  }

  public void init()
  {
    this.contextFile = new File(this.contextFileName);
    if (!this.contextFile.exists()) {
      this.contextFile.getParentFile().mkdirs();
      try {
        this.contextFile.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public void processFile(File file)
  {
    this.logFileRead.readAFile(file);
    saveToFile(this.contextFile, file.getName());
  }
  public void saveToFile(File file, String context) {
    BufferedWriter out = null;
    try {
      FileOutputStream fos = new FileOutputStream(file);
      DataOutputStream dos = new DataOutputStream(fos);
      out = new BufferedWriter(new OutputStreamWriter(dos));
      out.write(context);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      try
      {
        out.flush();
        out.close();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
      try
      {
        out.flush();
        out.close();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        out.flush();
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public String readFile(File file) {
    BufferedReader reader = null;
    FileReader fileReader = null;
    try {
      fileReader = new FileReader(file);
      reader = new BufferedReader(fileReader);
      return reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null)
        try {
          reader.close();
        }
        catch (IOException localIOException5) {
        }
      if (fileReader != null)
        try {
          fileReader.close();
        }
        catch (IOException localIOException6) {
        }
    }
    return null;
  }
}